var _captchaObj;

var forgetPasswordManager = {
	/**
	 * 找回密码第一步
	 */
	findPassFirstStep : function (){
		var option={
				rules:{
					regName:{
						required:true
					}/*,
					code:{
						required:true
					}*/
				},
				submit:{
					url:'/user/reg/findPassword.do',
					success:function(result){
						if(result.error.code == 0){
							window.location.href = "/user/reg/stepConfirm.do";
						}else if(result.error.code == 2){
							window.location.href = "/user/reg/findPassByPhone.do";
						}
					},
					failed:function(result){
						_captchaObj.refresh();
						error.handleError(result);
					}
				}
		};
		validator().validate(option);
	},

	/**
	 * 更新密码
	 */
    finalupdatePass: function(){

		validator().validate({
				rules:{
					newPassword:{
						required:true
					},
					reNewPassword:{
						required:true,
						equalTo:"newPassword",
						param:{
							name:""
						}
					},
					updatePwdPhoneCode:{
						required:true,
						messageCode:true
					},
					totpCode:{
						required:true,
						googleCode:true
					}
				},
				submit:{
					buttonId:'updatePassBtn',
					url:'/user/reg/resetPassword.do',
					success:function(result){
						window.location.href = "/user/reg/finishUpdatePassWord.do";
					}
				}
		});
	},
	
	/**
	 * 检查验手机证码
	**/
	checkMsgCode: function(){
		var option={
				rules:{
					msgCode:{
						required: true,
						messageCode: true,
						param:{
							name:"code"
						}
					}
				},
				submit:{
					buttonId:'checkMsgsubmitBtn',
					url:'/user/reg/checkPhoneVerifyCode.do',
					success:function(result){
						if(result.error.code == 0){
							window.location.href = "/user/reg/showResetPassword.do";
						}
					}
				}
				
		}
		validator().validate(option);
	},

	/**
	 * 完成修改密码跳转
	 */
	finishUpdatePass: function(){
		location.href='/user/reg/finishUpdatePassWord.do';
	}
};

function initCaptcha(config) {
	
	jQuery.post('/captcha/init.do', {}, function(data){
		
		var _config = $.extend({}, config);
		_config.challenge = data.challenge;
		
		initGeetest(_config, function (captchaObj) {

		    captchaObj.appendTo("#captcha"); // 绑定到id为captcha的元素上
		    
		    captchaObj.onSuccess(function () {
		        // 成功回调
		        jQuery.post('/captcha/verify.do',{
		        	'geetest_challenge' : jQuery(".geetest_challenge").val(),
		        	'geetest_validate' : jQuery(".geetest_validate").val(),
		        	'geetest_seccode' : jQuery(".geetest_seccode").val()
		        },function(data){
		        	if (data.indexOf('success') > -1) {
		        		
		        	} else if (data.indexOf('failure') > -1) {
		        		showNoteInfo(languageJson['error39']);
		        	} else {
		        		showNoteInfo(languageJson['error68']);
		        	}
		        });
		    }, 'text');
		    
		    captchaObj.getValidate(); // 获取成功后的验证信息，失败是返回false
		    
		    _captchaObj = captchaObj;
	    });
		
	}, 'json');
	
}


$(function(){
	//初始化手机验证码框
	$('#msgCode').initMessageCode({
		type: 13,
		phone:function(){
			return $("#phoneNum").val();
		},
		areaCode:function(){
			return $("#areaCode").val();
		}
	});

	$("#code").on('input',function(){
		var captcha = $("#code").val();
        var desc ="";
        $("#codeError").html("");
		if(captcha.length==4){
            // 校验验证码
            url = "/user/checkCaptcha.do?type=1&random="+Math.round(Math.random()*100);
            var param={ranCode:captcha};
            jQuery.post(url,param,function(resultData){
                var result = eval('(' + resultData + ')');
                if(result!=null){
                    var data = result.resultCode;
                }else{
                    var data = -2;
                }

                if(data==1){
                    $("#submitBtn").attr('disabled',false);

                }else if(data==-1){

                    desc = languageJson['smsisincorrect'];
                    //ranCodeSubmit.attr("disabled",true);
                }else if(data==-2){

                    desc = languageJson['smsisincorrect'];
                }else{

                    //ranCodeSubmit.attr("disabled","true");
                    desc= languageJson['smsisincorrect'];
                }
                if($("#codeError")!=null){
                    $("#codeError").html(desc);
                    window.setTimeout(function(){
                        jQuery("#codeError").html(" ");
                    },5000);
                }

            });
		}else if(captcha.length>4){
			$("#codeError").html(languageJson['smsisincorrect']);
		}

	});
	forgetPasswordManager.findPassFirstStep();
	forgetPasswordManager.finalupdatePass();
	forgetPasswordManager.checkMsgCode();
});
